package sm.hibernate;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.dam.api.DBDictService;
import sm.util.SMMap;

public class SMHibernateTemplate extends HibernateTemplate {

	public SMHibernateTemplate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	public Serializable save(final String entityName, final Object entity)
			throws DataAccessException {
		if (!DBDictService.DBDICT.equals(entityName)) {
			SMMap dataMap = (SMMap) entity;
			dataMap.put(DBDictService.DESCRIPTOR, dataMap.clone());
		}
		return super.save(entityName, entity);
	}

	@Override
	public void update(final String entityName, final Object entity) {
		if (!DBDictService.DBDICT.equals(entityName)) {
			SMMap dataMap = (SMMap) entity;
			dataMap.put(DBDictService.DESCRIPTOR, dataMap.clone());
		}
		super.update(entityName, entity);
	}
}
